
export function dynamicRouter(routers) {
    const list = []
    routers.forEach((itemRouter,index) => {
      list.push({
        ...itemRouter,
        component: ()=>import(`@/views${itemRouter.path}`)
      })
      // 是否存在子集
      if (itemRouter.children && itemRouter.children.length) {
        list[index].children = dynamicRouter(itemRouter.children);
      }
    })
  
    return list
  }



export function generateRouter(userRouters) {
  let newRouters;
  newRouters = userRouters.map(r => {
      let routes = {
          path: r.path,
          name: r.name,
          component: () => import(`../view/${r.name}.vue`)
      }
      if (r.children) {
          routes.children = generateRouter(r.children)
      }
      return routes
  });
  return newRouters
}


export function formatRouterTree(datas) {
  let res = []
  datas.map(data => {
      // data.children = []
      //console.log(data.children)
      if (data.pid === 0) {
          res.push(data)
      } else {
          add2Tree(data, res)
      }
  })
  return res

}

function add2Tree(data, res) {
  res.map(r => {
      if (data.pid === r.id) {
          if (!r.children) r.children = []
          r.children.push(data)
      } else {
          if (r.children)
              add2Tree(data, r.children)
      }
  })

}